<template>
  <div class="container">
    <div>
      <el-row :gutter="20">
        <el-col :span="6">
          <el-form ref="form" :model="form">
            <el-form-item label="公司名称：">
              <el-input style="width: 180px" v-model="form.CompanyName"></el-input>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="6">
          <el-form ref="form" :model="form">
            <el-form-item label="车辆名称：">
              <el-input style="width: 180px" v-model="form.LICENSE_PLATE"></el-input>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="6">
          <el-form ref="form" :model="form" style="float: left">
            <el-button size="mini" type="primary" @click="getlistdata"
              >查 询</el-button
            >
            <el-button size="mini" type="success" @click="addVehicleApproval"
              >新 增</el-button
            >
            <el-button size="mini" type="success" @click="exportExcel"
              >导 出</el-button
            >
          </el-form>
        </el-col>
      </el-row>
    </div>
    <el-table
      highlight-current-row
      v-loading="loading"
      :data="
        tableData.slice((currentPage - 1) * pageSize, currentPage * pageSize)
      "
    >
      
      <el-table-column
        prop="LICENSE_PLATE"
        label="车辆牌照"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="COMPANY"
        label="车辆所属公司"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="IsSetupGPSStr"
        label="是否安装定位"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="VIDEOStr"
        label="是否安装视频"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="StatusStr"
        label="审批结果"
        align="center"
      ></el-table-column>
      <el-table-column label="操作" width="100">
        
        <template slot-scope="scope">
          <el-button
            type="text"
            icon="el-icon-info"
            @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            type="text"
            icon="el-icon-info"
            @click="deletedata(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination
        background
        layout="total, sizes, prev, pager, next, jumper"
        :current-page="currentPage"
        :page-size="pageSize"
        :total="tableData.length"
        @size-change="handleSizeChange"
        @current-change="handlePageChange"
      ></el-pagination>
    </div>

    <!-- 弹出框 添加路线 -->
    <el-dialog
      :title="addLineDialogTitle"
      :visible.sync="addLineDialogShow"
      width="50%"
    >
      <el-form
        ref="formLine"
        :model="formLine"
        label-width="120px"
        class="flex"
      >
        <el-form-item label="车辆：" >
          <el-select
          :disabled="addLineDialogTitle.includes('编辑')"
            style="width: 177px"
            v-model="formLine.VID"
            :options="arrayCar"
            placeholder="请选择"
            filterable
            clearable
          >
            <el-option
              v-for="item in arrayCar"
              :key="item.VID"
              :label="item.LICENSE_PLATE"
              :value="item.VID"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="审批结果：">
          <el-select
            style="width: 177px"
            v-model="formLine.STATUS"
            :options="formStatus"
            placeholder="请选择"
            filterable
            clearable
          >
            <el-option
              v-for="item in formStatus"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addLineDialogShow = false">取 消</el-button>
        <el-button type="primary" @click="savedata()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import "@/assets/css/common.css";
import { formatDate } from "@/common/js/date.js";
import export2Excel from "@/utils/exportExcel/index.js";
export default {
  data () {
    return {
      currentPage: 1, //初始页
      pageSize: 10, //    每页的数据
      loading: false,
      form: {
        LICENSE_PLATE: '',
        CompanyName: '',
        StartDate: formatDate(
          new Date(new Date() - 3600 * 1000 * 24),
          "yyyy-MM-dd"
        ),
        EndDate: formatDate(new Date(), "yyyy-MM-dd"),
      },
      formLine: {
        ID: '-1',
        VID: '',
        STATUS: '0',
      },
      formStatus: [{
        label: '未过检',
        value: '0'
      }, {
        label: '已过检',
        value: '1'
      }],
      tableData: [],
      originalData: [],
      addLineDialogTitle: '',
      addLineDialogShow: false,
      arrayCar: [],
    };
  },
  created () {
    this.getCarData()
    this.getlistdata()
  },
  methods: {
    addVehicleApproval () {
      this.addLineDialogTitle = '新增运输资格'
      this.addLineDialogShow = true
      this.formLine = {
        ID: '-1',
        VID: '',
        STATUS: '0',
      }
    },
    async getlistdata () {
      // Vehicle/GetVehicleApprovalInfoZT?ClientNO=0000&CompanyName=&LicensePlate=&Status-1
      this.currentPage = 1;
      this.loading = true;
      var url = "Vehicle/GetVehicleApprovalInfoZT?CompanyName=&LicensePlate=&Status=-1&ClientNO=" + localStorage.getItem('CLIENT_NO')
      const { data } = await this.$Http.get(url);
      // this.tableData = data;
      this.tableData = []
      data.forEach(element => {
        if (!element.LICENSE_PLATE.includes(this.form.LICENSE_PLATE)) {
          return;
        }
        if (!element.COMPANY.includes(this.form.CompanyName)) {
          return;
        }
        if (element.STATUS == 0) {
          element.StatusStr = '未过检'
        } else {
          element.StatusStr = '已过检'
        }
        if (element.IsSetupGPS == '1') {
          element.IsSetupGPSStr = '已安装'
        } else {
          element.IsSetupGPSStr = '未安装'
        }
        if (element.VIDEO == '1') {
          element.VIDEOStr = '已安装'
        } else {
          element.VIDEOStr = '未安装'
        }
        this.tableData.push(element)
      });
      this.loading = false;
    },
    async savedata () {
      // Vehicle/SaveVehicleApprovalInfoZT?ID=&VID=&Status=
      var url = 'Vehicle/SaveVehicleApprovalInfoZT?ID=' + this.formLine.ID + '&VID=' + this.formLine.VID + '&Status=' + this.formLine.STATUS 
      const { data } = await this.$Http.get(url)
      if (data.IsSucess) {
        this.$message.success('新增成功');
        this.addLineDialogShow = false
        this.getlistdata()
      } else {
        this.$message.error('新增失败');
      }
    },
    async deletedata (index, row) {
      // Vehicle/SaveVehicleApprovalInfoZT?ID=&VID=&Status=
      var url = 'Vehicle/DeleteVehicleApprovalInfoZT?ID=' + row.ID
      const { data } = await this.$Http.get(url)
      if (data.IsSucess) {
        this.$message.success('删除成功');
        this.getlistdata()
      } else {
        this.$message.error('删除失败');
      }
    },
    async getCarData () {
      var url = 'Vehicle/GetVehicleInfoZT?CompanyName=&LicensePlate=&ClientNO=' + localStorage.getItem('CLIENT_NO')
      const { data } = await this.$Http.get(url)
      this.arrayCar = data
    },
    // 导出Excel
    exportExcel () {
      let excelName = "车辆运输资格审批";
      const filterVal = ["LICENSE_PLATE", "COMPANY", "IsSetupGPSStr", "VIDEOStr", "StatusStr"];
      const tHeader = [
        "车辆牌照",
        "公司名称",
        "是否安装定位",
        "是否安装视频",
        "审批结果",
      ];
      // ID审核ID,VID车辆ID,LICENSE_PLATE车辆牌照,COMPANY公司名称,IsSetupGPS是否安装定位（0未安装、1已安装）
      // ,VIDEO是否安装视频（0未安装、1已安装）,SETTIME设置时间,STATUS状态（0未过审、1已过审）
      export2Excel(tHeader, filterVal, this.tableData, excelName);
    },
    //详情
    handleEdit (index, row) {
      this.addLineDialogTitle = '编辑运输资格'
      this.addLineDialogShow = true
      this.formLine = Object.assign({}, row);
    },
    // 分页导航
    handlePageChange (currentPage) {
      this.currentPage = currentPage;
      // console.log(this.currentPage)
      // this.$set(this.query, 'pageIndex', val);
      // this.getData();
    },
    handleSizeChange (size) {
      this.pageSize = size;
      console.log(this.pageSize);
    },
  },
};
</script>
<style>
.content {
  overflow-y: scroll;
}
.el-col-6 {
  min-width: 400px;
  min-height: 50px;
}
.el-table th {
  height: 40px;
}
</style>